<?php
class ControllerCatalogReview extends Controller
{
	private $error = array();

	public function index()
	{
		$this->load->language('catalog/review');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('catalog/review');
		$this->getList();
	}

	public function insert()
	{
		$this->load->language('catalog/review');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('catalog/review');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm())
		{
			$this->model_catalog_review->addReview($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$url                            = '';

			if (isset($this->request->get['sort']))
			{
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order']))
			{
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page']))
			{
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('catalog/review', $url, 'SSL'));
		}

		$this->getForm();
	}

	public function update()
	{
		$this->load->language('catalog/review');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('catalog/review');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm())
		{
			$this->model_catalog_review->editReview($this->request->get['review_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$url                            = '';

			if (isset($this->request->get['sort']))
			{
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order']))
			{
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page']))
			{
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('catalog/review', $url, 'SSL'));
		}

		$this->getForm();
	}

	public function delete()
	{
		$this->load->language('catalog/review');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('catalog/review');

		if (isset($this->request->post['selected']) && $this->validateDelete())
		{
			foreach ($this->request->post['selected'] as $review_id)
			{
				$this->model_catalog_review->deleteReview($review_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');
			$url                            = '';

			if (isset($this->request->get['sort']))
			{
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order']))
			{
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page']))
			{
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('catalog/review', $url, 'SSL'));
		}

		$this->getList();
	}

	private function getList()
	{
		if (isset($this->request->get['sort']))
		{
			$sort = $this->request->get['sort'];
		}
		else
		{
			$sort = 'r.date_added';
		}

		if (isset($this->request->get['order']))
		{
			$order = $this->request->get['order'];
		}
		else
		{
			$order = 'ASC';
		}

		$page = isset($this->request->get['page']) ? $this->request->get['page'] : 1;

		$url = '';

		if (isset($this->request->get['sort']))
		{
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order']))
		{
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page']))
		{
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs']   = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
			'separator' => false
		);
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('catalog/review', $url, 'SSL'),
			'separator' => ' :: '
		);
		$this->data['insert']        = $this->url->link('catalog/review/insert', $url, 'SSL');
		$this->data['delete']        = $this->url->link('catalog/review/delete', $url, 'SSL');
		$this->data['reviews']       = array();
		$data                        = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$review_total                = $this->model_catalog_review->getTotalReviews();
		$results                     = $this->model_catalog_review->getReviews($data);

		foreach ($results as $result)
		{
			$action                  = array();
			$action[]                = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('catalog/review/update', '&review_id=' . $result['review_id'] . $url, 'SSL')
			);
			$this->data['reviews'][] = array(
				'review_id'  => $result['review_id'],
				'name'       => $result['name'],
				'author'     => $result['author'],
				'rating'     => $result['rating'],
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
				'selected'   => isset($this->request->post['selected']) && in_array($result['review_id'], $this->request->post['selected']),
				'action'     => $action
			);
		}

		$this->data['heading_title']     = $this->language->get('heading_title');
		$this->data['text_no_results']   = $this->language->get('text_no_results');
		$this->data['column_product']    = $this->language->get('column_product');
		$this->data['column_author']     = $this->language->get('column_author');
		$this->data['column_rating']     = $this->language->get('column_rating');
		$this->data['column_status']     = $this->language->get('column_status');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_action']     = $this->language->get('column_action');
		$this->data['button_insert']     = $this->language->get('button_insert');
		$this->data['button_delete']     = $this->language->get('button_delete');

		$this->data['error_warning'] = isset($this->error['warning']) ? $this->error['warning'] : '';

		if (isset($this->session->data['success']))
		{
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		}
		else
		{
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC')
		{
			$url .= '&order=DESC';
		}
		else
		{
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page']))
		{
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['sort_product']    = $this->url->link('catalog/review', '&sort=pd.name' . $url, 'SSL');
		$this->data['sort_author']     = $this->url->link('catalog/review', '&sort=r.author' . $url, 'SSL');
		$this->data['sort_rating']     = $this->url->link('catalog/review', '&sort=r.rating' . $url, 'SSL');
		$this->data['sort_status']     = $this->url->link('catalog/review', '&sort=r.status' . $url, 'SSL');
		$this->data['sort_date_added'] = $this->url->link('catalog/review', '&sort=r.date_added' . $url, 'SSL');
		$url                           = '';

		if (isset($this->request->get['sort']))
		{
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order']))
		{
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination               = new Pagination();
		$pagination->total        = $review_total;
		$pagination->page         = $page;
		$pagination->limit        = $this->config->get('config_admin_limit');
		$pagination->text         = $this->language->get('text_pagination');
		$pagination->url          = $this->url->link('catalog/review', $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		$this->data['sort']       = $sort;
		$this->data['order']      = $order;
		$this->template           = 'template/catalog/review_list.tpl';
		$this->children           = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function getForm()
	{
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_enabled']  = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_none']     = $this->language->get('text_none');
		$this->data['text_select']   = $this->language->get('text_select');
		$this->data['entry_product'] = $this->language->get('entry_product');
		$this->data['entry_author']  = $this->language->get('entry_author');
		$this->data['entry_rating']  = $this->language->get('entry_rating');
		$this->data['entry_status']  = $this->language->get('entry_status');
		$this->data['entry_text']    = $this->language->get('entry_text');
		$this->data['entry_good']    = $this->language->get('entry_good');
		$this->data['entry_bad']     = $this->language->get('entry_bad');
		$this->data['button_save']   = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['error_warning'] = isset($this->error['warning']) ? $this->error['warning'] : '';
		if (isset($this->error['product']))
		{
			$this->data['error_product'] = $this->error['product'];
		}
		else
		{
			$this->data['error_product'] = '';
		}

		if (isset($this->error['author']))
		{
			$this->data['error_author'] = $this->error['author'];
		}
		else
		{
			$this->data['error_author'] = '';
		}

		if (isset($this->error['text']))
		{
			$this->data['error_text'] = $this->error['text'];
		}
		else
		{
			$this->data['error_text'] = '';
		}

		if (isset($this->error['rating']))
		{
			$this->data['error_rating'] = $this->error['rating'];
		}
		else
		{
			$this->data['error_rating'] = '';
		}

		$url = '';

		if (isset($this->request->get['sort']))
		{
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order']))
		{
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page']))
		{
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs']   = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
			'separator' => false
		);
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('catalog/review', $url, 'SSL'),
			'separator' => ' :: '
		);

		if (!isset($this->request->get['review_id']))
		{
			$this->data['action'] = $this->url->link('catalog/review/insert', $url, 'SSL');
		}
		else
		{
			$review_info = $this->model_catalog_review->getReview($this->request->get['review_id']);
			$this->data['action'] = $this->url->link('catalog/review/update', '&review_id=' . $this->request->get['review_id'] . $url, 'SSL');
		}

		$this->data['cancel'] = $this->url->link('catalog/review', $url, 'SSL');

		$this->load->model('catalog/product');

		if (isset($this->request->post['product_id']))
		{
			$this->data['product_id'] = $this->request->post['product_id'];
		}
		elseif (!empty($review_info))
		{
			$this->data['product_id'] = $review_info['product_id'];
		}
		else
		{
			$this->data['product_id'] = '';
		}

		if (isset($this->request->post['product']))
		{
			$this->data['product'] = $this->request->post['product'];
		}
		elseif (!empty($review_info))
		{
			$this->data['product'] = $review_info['product'];
		}
		else
		{
			$this->data['product'] = '';
		}

		if (isset($this->request->post['author']))
		{
			$this->data['author'] = $this->request->post['author'];
		}
		elseif (!empty($review_info))
		{
			$this->data['author'] = $review_info['author'];
		}
		else
		{
			$this->data['author'] = '';
		}

		if (isset($this->request->post['text']))
		{
			$this->data['text'] = $this->request->post['text'];
		}
		elseif (!empty($review_info))
		{
			$this->data['text'] = $review_info['text'];
		}
		else
		{
			$this->data['text'] = '';
		}

		if (isset($this->request->post['rating']))
		{
			$this->data['rating'] = $this->request->post['rating'];
		}
		elseif (!empty($review_info))
		{
			$this->data['rating'] = $review_info['rating'];
		}
		else
		{
			$this->data['rating'] = '';
		}

		if (isset($this->request->post['status']))
		{
			$this->data['status'] = $this->request->post['status'];
		}
		elseif (!empty($review_info))
		{
			$this->data['status'] = $review_info['status'];
		}
		else
		{
			$this->data['status'] = '';
		}

		$this->template = 'template/catalog/review_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function validateForm()
	{
		if (!$this->user->hasPermission('modify', 'catalog/review'))
		{
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->request->post['product_id'])
		{
			$this->error['product'] = $this->language->get('error_product');
		}

		if ((mb_strlen($this->request->post['author']) < 3) || (mb_strlen($this->request->post['author']) > 64))
		{
			$this->error['author'] = $this->language->get('error_author');
		}

		if (mb_strlen($this->request->post['text']) < 1)
		{
			$this->error['text'] = $this->language->get('error_text');
		}

		if (!isset($this->request->post['rating']))
		{
			$this->error['rating'] = $this->language->get('error_rating');
		}

		return (!$this->error) ? true : false;
	}

	private function validateDelete()
	{
		if (!$this->user->hasPermission('modify', 'catalog/review'))
		{
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return (!$this->error) ? true : false;
	}
}

?>